package org.cogaen.motion;

public class Vector {

	public double x;
	public double y;
	
	public Vector() {
		this.x = 0.0;
		this.y = 0.0;
	}
	
	public Vector(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	public Vector(Vector v1) {
		this.x = v1.x;
		this.y = v1.y;
	}

	public void normalize(Vector v1) {
		double length = v1.length();
		this.x = v1.x / length;
		this.y = v1.y / length;
	}
	
	public void normalize() {
		double length = length();
		if (length != 0.0) {
			this.x /= length;
			this.y /= length;
		}
	}
	
	public double length() {
		return Math.sqrt(lengthSquared());
	}
	
	public double lengthSquared() {
		return this.x * this.x + this.y * this.y;
	}
		
	public void add(Vector v1) {
		this.x += v1.x;
		this.y += v1.y;
	}
	
	public void add(Vector v1, Vector v2) {
		this.x = v1.x + v2.x;
		this.y = v1.y + v2.y;
	}
	
	public void sub(Vector v1) {
		this.x -= v1.x;
		this.y -= v1.y;
	}
	
	public void sub(Vector v1, Vector v2) {
		this.x = v1.x - v2.x;
		this.y = v1.y - v2.y;
	}
	
	public void set(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	public void set(Vector v1) {
		this.x = v1.x;
		this.y = v1.y;
	}
	
	public void scale(double s) {
		this.x *= s;
		this.y *= s;
	}
	
	public void scale(double s, Vector v1) {
		this.x = v1.x * s;
		this.y = v1.y * s;
	}
	
	/**
	 * Sets the value of this vector to the scalar multiplication of itself and
	 * then adds vector v1 (this = s*this + v1). 
	 * 
	 * @param s the scalar value
	 * @param v1 the vector to be added
	 */
	public void scaleAdd(double s, Vector v1) {
		this.x = this.x * s + v1.x;
		this.y = this.y * s + v1.y;
	}
	
	/**
	 * Sets the value of this vector to the scalar multiplication of vector v1
	 * and then adds vector v2 (this = s*v1 + v2). 
	 * 
	 * @param s the scalar value
	 * @param v1 the vector to be multipled
	 * @param v2 the vector to be added
	 */
	public void scaleAdd(double s, Vector v1, Vector v2) {
		this.x = v1.x * s + v2.x;
		this.y = v1.y * s + v2.y;
	}
	
	public double dot(Vector v1) {
		return this.x * v1.x + this.y * v1.y;
	}
	
	public void negate() {
		this.x = -this.x;
		this.y = -this.y;
	}
	
	public void negate(Vector v1) {
		this.x = -v1.x;
		this.y = -v1.y;
	}
	
}
